<?php

session_start();
require('connection.php');

/* This is where all functions are stored */


function getAllEmployees($filter=0) {
	
	establishConnection();
	
	$query  = "CALL sp_employee_select($filter)";
	$result = mysql_query($query);
	
	return $result ? convertToObject($result):false;
	
}	

function getEmployee($id) {
	
	establishConnection();
	
	$query  = "CALL sp_employee_view('$id')";
	$result = mysql_query($query);
	
	return $result ? convertToObject($result):false;
	
}	

function getAllDepartments() {
	
	establishConnection();
	
	$query  = "CALL sp_department_select()";
	$result = mysql_query($query) OR die(mysql_error());
	
	return $result ? convertToObject($result):false;
}	

function getAllJobs() {
	
	establishConnection();
	
	$query  = "CALL sp_job_select()";
	$result = mysql_query($query) OR die(mysql_error());
	
	return $result ? convertToObject($result):false;
}

function getAllJobStatus() {
	
	establishConnection();
	
	$query  = "CALL sp_status_select()";
	$result = mysql_query($query) OR die(mysql_error());
	
	return $result ? convertToObject($result):false;
}

function getAllLogs() {
	
	establishConnection();
	
	$query  = "CALL sp_log_select()";
	$result = mysql_query($query) OR die(mysql_error());
	
	return $result ? convertToObject($result):false;
}

function updateEmployee($id, $name, $address, $contact, $email, $gender, $birthdate, $department, $job, $status) {
	establishConnection();
	
	$args = func_get_args();
	
	foreach ($args as $i => $arg) {
		$args[$i] = mysql_real_escape_string($arg);
	}
	
	$query  = "CALL sp_employee_update('".$args[1]."', '".$args[2]."', '".$args[3]."', '".$args[4]."', '".$args[5]."', '".$args[6]."', ".$args[7].", ".$args[8].", ".$args[9].", '".$args[0]."')";
	$result = mysql_query($query) OR die(mysql_error());
}

function addEmployee($name, $address, $contact, $email, $gender, $birthdate, $department, $job, $status) {
	establishConnection();
	
	$args = func_get_args();
	
	foreach ($args as $i => $arg) {
		$args[$i] = mysql_real_escape_string($arg);
	}
	
	$query  = "CALL sp_employee_add('".$args[0]."', '".$args[1]."', '".$args[2]."', '".$args[3]."', '".$args[4]."', '".$args[5]."', ".$args[6].", ".$args[7].", ".$args[8].")";
	$result = mysql_query($query) OR die(mysql_error());
}

function convertToObject($result) {

	$list = array();
	
	while ($row = mysql_fetch_object($result)) {
		$list[] = $row;
	}
	
	mysql_close();
	
	return $list ? $list:false;
	
}

function getDateFields($date=null) {
	$date = !is_null($date) ? explode("-", $date):null;
	$string = "";
	
	$months = array(1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	
	// Month
	$string .= "<select name='month'>";
	foreach ($months as $i => $month) {
		if (!is_null($date)) {
			if ($i == $date[1]) {
				$string .= "<option selected='selected' value='$i'>$month</option>";
			}
			else {
				$string .= "<option value='$i'>$month</option>";
			}
		}
		else {
			$string .= "<option value='$i'>$month</option>";
		}
	}
	$string .= "</select>";
	
	// Day
	$string .= "<select name='day'>";
	for ($i=1; $i<=31; $i++) {
		if (!is_null($date)) {
			if ($i == $date[2]) {
				$string .= "<option selected='selected' value='$i'>$i</option>";
			}
			else {
				$string .= "<option value='$i'>$i</option>";
			}
		}
		else {
			$string .= "<option value='$i'>$i</option>";
		}
	}
	$string .= "</select>";
	
	// Year
	$string .= "<select name='year'>";
	for ($i=date("Y", time()); $i>=1950; $i--) {
		if (!is_null($date)) {
			if ($i == $date[0]) {
				$string .= "<option selected='selected' value='$i'>$i</option>";
			}
			else {
				$string .= "<option value='$i'>$i</option>";
			}
		}
		else {
			$string .= "<option value='$i'>$i</option>";
		}
	}
	$string .= "</select>";
	
	
	return $string;
}

function getDepartmentFields($department_id=null) {
	
	$departments = getAllDepartments();
	
	$string = "";
	
	$string .= "<select name='department'>";
	foreach ($departments as $department) {
		if (!is_null($department_id) && $department_id == $department->id) {
			$string .= "<option selected='selected' value='".$department->id."'>".$department->name."</option>";
		}
		else {
			$string .= "<option value='".$department->id."'>".$department->name."</option>";
		}
	}
	$string .= "</select>";
	
	return $string;
}

function getJobFields($job_id=null) {
	
	$jobs = getAllJobs();
	
	$string = "";
	
	$string .= "<select name='job'>";
	foreach ($jobs as $job) {
		if (!is_null($job_id) && $job_id == $job->id) {
			$string .= "<option selected='selected' value='".$job->id."'>".$job->name."</option>";
		}
		else {
			$string .= "<option value='".$job->id."'>".$job->name."</option>";
		}
	}
	$string .= "</select>";
	
	return $string;
}

function getJobStatusFields($job_status=null) {
	
	$status = getAllJobStatus();
	
	$string = "";
	
	$string .= "<select name='status'>";
	foreach ($status as $job) {
		if (!is_null($job_status) && $job_status == $job->id) {
			$string .= "<option selected='selected' value='".$job->id."'>".$job->name."</option>";
		}
		else {
			$string .= "<option value='".$job->id."'>".$job->name."</option>";
		}
	}
	$string .= "</select>";
	
	return $string;
}



?>